package com.zdzh.socketio.socket.session;

import com.corundumstudio.socketio.SocketIOClient;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class ClientSessionHolder {

    private static final ConcurrentHashMap<String, SocketIOClient> serverSessions = new ConcurrentHashMap<>();

    public static void insert(SocketIOClient socketIOClient) {
        String user_id = socketIOClient.getHandshakeData().getSingleUrlParam("userId");
        String tenant_id = socketIOClient.getHandshakeData().getSingleUrlParam("tenantId");
        String key = tenant_id + "@" + user_id;
        log.info("创建socketClient成功, tenantId:{}, userId:{} ", tenant_id, user_id);
        serverSessions.put(key, socketIOClient);
    }

    public static Optional<SocketIOClient> select(String key) {
        return Optional.ofNullable(serverSessions.get(key));
    }

    public static void delete(SocketIOClient socketIOClient) {
        String user_id = socketIOClient.getHandshakeData().getSingleUrlParam("userId");
        String tenant_id = socketIOClient.getHandshakeData().getSingleUrlParam("userId");
        String key = tenant_id + "@" + user_id;
        serverSessions.remove(key);
    }

    public static ConcurrentHashMap<String, SocketIOClient> selectAll() {
        return serverSessions;
    }

}
